package com.braveman.im.ui.activity.message;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import com.braveman.im.R;
import com.braveman.im.bean.FriendGroupNameBean;
import com.braveman.im.manager.ApiManager;
import com.braveman.im.ui.adapter.AddFriendsGroupNameNewAdapter;
import com.braveman.im.ui.weiget.InputPopup;
import com.braveman.im.utils.UtilsStyle;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ht.baselibs.core.EventCode;
import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseActivity;
import com.ht.baselibs.ui.widget.TitleBarView;
import com.ht.baselibs.utils.ActivityUtils;
import com.ht.baselibs.utils.UIUtils;
import com.vondear.rxtools.RxBarTool;

import java.util.ArrayList;
import java.util.List;

import ezy.ui.layout.LoadingLayout;
import retrofit2.Call;
import retrofit2.Response;

public class SelectFriendsGroupNameActivity extends BaseActivity {
    private RecyclerView mLvActivityListMember;
    private LoadingLayout loadingLayout;
    private AddFriendsGroupNameNewAdapter addFriendsGroupNameAdapter;
    private List<FriendGroupNameBean> list = new ArrayList<>();
    private String userFriendCategoryName;
    private String userFriendCategoryId;
    private String friendId;

    @Override
    protected void initTitleBar() {
        mTitleBarView.setTitleText("移至分组").addActions(new TitleBarView.Action(R.color.color_19, "完成") {
            @Override
            public void performAction(View view) {
                ActivityUtils.finishActivityTopToBottom(SelectFriendsGroupNameActivity.this);
            }
        });
    }

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected void initIntentData(Intent callIntent) {
        userFriendCategoryName = callIntent.getStringExtra("userFriendCategoryName");
        userFriendCategoryId = callIntent.getStringExtra("userFriendCategoryId");
        friendId = callIntent.getStringExtra("friendId");
    }

    @Override
    protected int initContentView() {

        UtilsStyle.setStatusBarMode(this, true);
        RxBarTool.FlymeSetStatusBarLightMode(getWindow(), true);
        RxBarTool.MIUISetStatusBarLightMode(getWindow(), true);
        return R.layout.activity_add_friends_group_name_new;
    }

    @Override
    protected void initChildView() {
        addFriendsGroupNameAdapter = new AddFriendsGroupNameNewAdapter(R.layout.item_friends_group_name_new, list);
        loadingLayout = findViewById(R.id.loading_layout);
        mLvActivityListMember = findViewById(R.id.lv_Activity_list_member);
        mLvActivityListMember.setLayoutManager(new LinearLayoutManager(SelectFriendsGroupNameActivity.this));

        View headerView = getHeaderView(0, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InputPopup inputPopup = new InputPopup(AddFriendsAndGroupActivity.currentActivity);
                inputPopup.showPopupWindow();
            }
        });

        addFriendsGroupNameAdapter.addHeaderView(headerView);
        mLvActivityListMember.setAdapter(addFriendsGroupNameAdapter);
    }

    @Override
    protected void initChildData() {
        if (TextUtils.isEmpty(userFriendCategoryName)) {
            userFriendCategoryName = "我的好友";
        }
        doNetWork();
    }

    private void doNetWork() {
        loadingLayout.showLoading();
        ApiManager.getApi().getFriendGroupName().enqueue(new NetCallBack<List<FriendGroupNameBean>>() {
            @Override
            protected void onSuccess(Call<List<FriendGroupNameBean>> call, Response<List<FriendGroupNameBean>> response) {
                if (response != null) {
                    if (list != null && list.size() > 0) {
                        list.clear();
                    }
                    list.addAll(response.body());
                    for (FriendGroupNameBean friendGroupNameBean : list) {

                        // TODO: 17/11/10 此处要用userFriendCategoryId 做判断
                        if (userFriendCategoryName.equals(friendGroupNameBean.name)) {
                            friendGroupNameBean.isShow = true;
                        }
                    }

                    if (list.size() == 0) {
                        loadingLayout.showEmpty();
                    } else {
                        loadingLayout.showContent();
                    }
                    if (addFriendsGroupNameAdapter != null) {
                        addFriendsGroupNameAdapter.notifyDataSetChanged();
                    }

                } else {
                    loadingLayout.showError();
                }
            }
        });
    }

    @Override
    protected void setChildViewListener() {
        addFriendsGroupNameAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                ApiManager.getApi().changeFriendGroupName(friendId, list.get(position).id).enqueue(new NetCallBack<FriendGroupNameBean>() {
                    @Override
                    protected void onSuccess(Call<FriendGroupNameBean> call, Response<FriendGroupNameBean> response) {
                        if (response != null) {
                            FriendGroupNameBean friendGroupNameBean = response.body();
                            UIUtils.showToast("修改分组成功");
                            if (friendGroupNameBean != null) {
                                Intent in = new Intent();
                                in.putExtra("name", friendGroupNameBean.userFriendCategoryName);
                                setResult(RESULT_OK, in);
                                ActivityUtils.finishActivity(SelectFriendsGroupNameActivity.this);
                            }
                        }
                    }
                });
            }
        });
    }

    private View getHeaderView(int type, View.OnClickListener listener) {
        View view = getLayoutInflater().inflate(R.layout.item_friends_group_name_head, (ViewGroup) mLvActivityListMember.getParent(), false);
        view.setOnClickListener(listener);
        return view;
    }

    @Override
    protected boolean hasEvent() {
        return true;
    }

    @Override
    protected void onEvent(int eventCode, Object data) {
        switch (eventCode) {
            case EventCode.EVENT_ADD_GROUP_NAME:
                doNetWork();
                break;
            case EventCode.EVENT_DELETE_GROUP_NAME:
                doNetWork();
                break;
        }
    }
}
